package com.seeyoui.kensite.framework.file.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.seeyoui.kensite.common.constants.StringConstant;
import com.seeyoui.kensite.common.util.RequestResponseUtil;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.PropertyResourceBundle;

/**
 *
 * @author yudian-it
 * @date 2017/12/1
 */
@Controller
@RequestMapping(value = "file")
public class FileController {
    PropertyResourceBundle prb = (PropertyResourceBundle) PropertyResourceBundle.getBundle("file-management");
    String fileDir = prb.getString("file.upload.url");// 文件上传绝对路径路径
    String demoPath = "demo" + File.separator;

    /**
     * 文件上传的方法
     * 
     * @param file
     * @param request
     * @return
     * @throws JsonProcessingException
     */
    @RequestMapping(value = "fileUpload", method = RequestMethod.POST)
    @ResponseBody
    public String fileUpload(@RequestParam("file") MultipartFile file, HttpSession session, HttpServletResponse response, HttpServletRequest request,
            ModelMap modelMap) throws JsonProcessingException {
        String fileName = file.getOriginalFilename();
        File outFile = new File(fileDir+demoPath);
        if (!outFile.exists()) {
            outFile.mkdirs();
        }
        try (InputStream in = file.getInputStream(); OutputStream ot = new FileOutputStream(fileDir+demoPath + fileName)) {
            byte[] buffer = new byte[1024];
            int len;
            while ((-1 != (len = in.read(buffer)))) {
                ot.write(buffer, 0, len);
            }
            modelMap.put("message", "文件上传成功");
            RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        } catch (IOException e) {
            e.printStackTrace();
            modelMap.put("message", "文件上传失败");
            RequestResponseUtil.putResponseStr(session, response, request, modelMap, StringConstant.TRUE);
        }
        return null;
    }

    @RequestMapping(value = "deleteFile", method = RequestMethod.GET)
    @ResponseBody
    public String deleteFile(String fileName, HttpServletRequest request) throws JsonProcessingException {
        if (fileName.contains("/")) {
            fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
        }
        File file = new File(fileDir+demoPath + fileName);
        if (file.exists()) {
            file.delete();
        }
        return null;
    }

    /**
     * 获取所有上传文件
     * 
     * @param response
     * @param request
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list/files", method = RequestMethod.POST)
    @ResponseBody
    public String listFiles(HttpServletResponse response, HttpServletRequest request) throws Exception {
        List<Map<String, String>> list = Lists.newArrayList();
        File file = new File(fileDir+demoPath);
        if (file.exists()) {
            Arrays.stream(file.listFiles()).forEach(file1 -> list.add(ImmutableMap.of("fileName", file1.getName())));
        }
        return new ObjectMapper().writeValueAsString(list);
    }

}
